<?php
	include_once "header.php";
	
	
		//分页代码======================================================
	//include_once("config.php");
	require_once('page.class.php'); //分页类
	$showrow = 24; //一页显示的行数
	$curpage = empty($_GET['page']) ? 1 : $_GET['page']; //当前的页,还应该处理非数字的情况
	
	$url = "?page={page}"; //分页地址，如果有检索条件 ="?page={page}&q=".$_GET['q']
	
	//获取是否有分类
	$cid = isset($_GET['cid']) && !empty($_GET['cid']) ? $_GET['cid'] : 0;
	if($cid > 0){
		$sql = 'select count(*) from kp_goods where gclass = '.$cid.' and gzt = 0';
		$url .="&cid={$cid}";
	}
	else
		$sql = 'select count(*) from kp_goods where gzt = 0';
	//echo $sql;
	//是否有搜索商品
	if(!empty($_GET['search'])){
		$mysearch = str_replace(" ", "%", trim($_GET['search']));
		$sql = 'select count(*) from kp_goods where gname like \'%'.$mysearch.'%\'';
		//$url = "?search={$mysearch}&page={page}";
		$url .="&search={$mysearch}";
	}
	//排序功能
	if(!empty($_GET['new'])){
		$url .="&new={$_GET['new']}";
	}elseif(!empty($_GET['zk'])){
		$url .="&zk={$_GET['zk']}";
	}elseif(!empty($_GET['price'])){
		$url .="&price={$_GET['price']}";
	}elseif(!empty($_GET['sell'])){
		$url .="&sell={$_GET['sell']}";
	}
	
	
	$qsum = $pdo->query($sql);
	$rows = $qsum->fetch();
	$total = $rows[0]; //获取总行数
	if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow))
    $curpage = ceil($total / $showrow); //当前页数大于最后页数，取最后一页
	//echo "<br>".$total;
	//分页代码完=====================================================
	
	
	
	
	//如果GET中有参数,则应进行判断:re,new,zk,price,sell,search,cid
	//新品
	//$stmt = $pdo->prepare('select gid,gname,gphoto,price,zkprice,newdate,num from kp_goods where gzt=0 order by newdate desc,gid desc limit 4');
	//$stmt->execute();
	//$arrNews = $stmt->fetchAll(PDO::FETCH_ASSOC);
	$sqlstr = 'select a.gid,gname,gclass,gphoto,price,zkprice,newdate,b.num from kp_goods a left join (select gid,sum(num) num from kp_sell group by gid) b on a.gid = b.gid where a.gzt=0 ';
/*	if(!empty($_GET)){
	}*/
	if(!empty($_GET['search'])){
		//$sqlstr.= ' and gname like \'%'.$_GET['search'].'%\' ';
		$sqlstr.= " and gname like ?";
	}
	if(!empty($_GET['cid'])){
		//$sqlstr.= ' and gname like \'%'.$_GET['search'].'%\' ';
		$sqlstr.= " and gclass like ?";
	}
	
	
	//如果有排序,则启用.
	if(!empty($_GET['new'])){
		$sqlstr.= ' order by newdate desc,gid desc';
	}elseif(!empty($_GET['zk'])){
		$sqlstr.= ' order by (price-zkprice) desc,gid desc';
	}elseif(!empty($_GET['price'])){
		$sqlstr.= ' order by zkprice asc,gid desc';
	}elseif(!empty($_GET['sell'])){
		$sqlstr.= ' order by num desc,gid desc';
	}
	$sqlstr .= ' limit '.($curpage-1)*$showrow.','.$showrow;
	
	$stmt = $pdo->prepare($sqlstr);
	if(!empty($_GET['search'])){
		//$stmt->bindValue(':search', '%'.$_GET['search'].'%', PDO::PARAM_STR);
		$stmt->execute(array('%'.$_GET['search'].'%'));
	}elseif(!empty($_GET['cid'])){
		$stmt->execute(array($_GET['cid'].'%'));
	}else{
		$stmt->execute();
	}
	
	//$stmt->execute();
	$arrAll = $stmt->fetchAll(PDO::FETCH_ASSOC);
	//print_r($arrAll);
?>

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<link rel="stylesheet" type="text/css" href="css/index.css"/>
	<title>商品分类列表</title>
	<script src="js/jquery-3.1.1.js" type="text/javascript" charset="utf-8"></script>
	<script src="js/info.js" type="text/javascript" charset="utf-8"></script>
	<style type="text/css">
		.going{
			width: 40px;
		}
	</style>
</head>
<body>
	<div id="index">
					<!--各种推荐-->
			<div class="goods">
				<div class="title">
					<h2><a href="">您的位置: 分类列表</a></h2>
					<div class="move">

						<a href="goodslist.php?<?php  
							if(!empty($_GET['search'])){
								echo 'search='.$_GET['search'].'&'.'new=100';
							}else if(!empty($_GET['cid'])){
								echo 'cid='.$_GET['cid'].'&'.'new=100';
							}else{
								echo 'new=100';
							}
						?>">新品</a>
						<a href="goodslist.php?<?php  
							if(!empty($_GET['search'])){
								echo 'search='. $_GET['search'].'&'.'sell=100';
							}else if(!empty($_GET['cid'])){
								echo 'cid='.$_GET['cid'].'&'.'sell=100';
							}else{
								echo 'sell=100';
							}
						?>">热销</a>
						<a href="goodslist.php?<?php  
							if(!empty($_GET['search'])){
								echo 'search='. $_GET['search'].'&'.'zk=100';
							}else if(!empty($_GET['cid'])){
								echo 'cid='.$_GET['cid'].'&'.'zk=100';
							}else{
								echo 'zk=100';
							}
						?>">折扣</a>
						<a href="goodslist.php?<?php  
							if(!empty($_GET['search'])){
								echo 'search='. $_GET['search'].'&'.'price=100';
							}else if(!empty($_GET['cid'])){
								echo 'cid='.$_GET['cid'].'&'.'price=100';
							}else{
								echo 'price=100';
							}
						?>">价格</a>
					</div>
				</div>
				<div class="goods_re">
					<!--商品-->
					<!--商品-->
					<?php
					 foreach($arrAll as $good){
					 	$gphotos = [];
						if(!empty($good['gphoto'])){
							$gphotos = explode(';', $good['gphoto']);
						}
					?>
					<div class="good">
						<img src="<?php if(!empty($gphotos[0])) echo  $gphotos[0]; else echo 'upload/good_001.gif'; ?>"/>
						<h4 class="title" title="<?php echo $good['gname']; ?>"><?php echo $good['gname']; ?></h4>
						<ul class="info">
							<li class="pricenow"><span class="yuan">¥</span><?php echo $good['zkprice']; ?></li>
							<li class="pricedel">¥<?php echo $good['price']; ?></li>
							<li class="incar" title="加入到购物车" onclick="<?php if($uid > 0) echo 'incar(\''.$good['gid'].'\',\''.$uid.'\',1)'; else echo 'location.href = \'login.php\''; ?>">+</li>
							
							<a href="info.php?gid=<?php echo $good['gid']; ?>"><li class="going" style="width: 40px;">购买</li></a>
						</ul>
					</div>
					<?php
					 }
					?>
					<!--商品-->
					
				</div>
			</div><!--各种推荐end-->
			
			<!--商品底部分页-->
			<?php
                if ($total > $showrow) {//总记录数大于每页显示数，显示分页
                    $page = new page($total, $showrow, $curpage, $url, 2);
                    echo $page->myde_write();
                }
            ?>
	</div>
	<?php include_once 'bottom.php' ?>
</body>
</html>
